package com.sky.service.impl;

import com.sky.entity.Orders;
import com.sky.mapper.OrdersMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.ReportService;
import com.sky.vo.OrderReportVO;
import com.sky.vo.SalesTop10ReportVO;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.BinaryOperator;

@Service
@Slf4j
public class ReportServiceImpl implements ReportService {
    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public TurnoverReportVO turnoverStatistics(LocalDate begin, LocalDate end) {
        // 日期列表
        List<LocalDate> dateList = new ArrayList<>();
        List<Double> turnoverList = new ArrayList<>();
        while (!begin.isAfter(end)) {
            dateList.add(begin);
            // 获取营业额 时间 begin 00:00 ~ 23:59:999992222222
            // 营业额列表

            Map map = new HashMap();
            map.put("status", Orders.COMPLETED);
            map.put("begin", LocalDateTime.of(begin, LocalTime.MIN));
            map.put("end", LocalDateTime.of(begin, LocalTime.MAX));
            // 根据上述条件获取营业额
            Double turnover = ordersMapper.getTurnover(map);
            turnover = turnover == null ? 0.0 : turnover;
            begin = begin.plusDays(1);
            turnoverList.add(turnover);
        }

        return TurnoverReportVO.builder()
                .dateList(StringUtils.join(dateList, ","))
                .turnoverList(StringUtils.join(turnoverList, ","))
                .build();
    }

    @Override
    public UserReportVO userStatistics(LocalDate begin, LocalDate end) {
        List<LocalDate> dateList = new ArrayList<>();
        List<Integer> totalUserList = new ArrayList<>();
        List<Integer> newUserList = new ArrayList<>();

        while (!begin.isAfter(end)) {
            dateList.add(begin);
            Map map = new HashMap();
            map.put("begin", LocalDateTime.of(begin, LocalTime.MIN));
            map.put("end", LocalDateTime.of(begin, LocalTime.MAX));
            // 获取每日新增用户
            Integer newUserNumber = userMapper.getUserNumber(map);
            newUserList.add(newUserNumber);
            // 获取总用户
            map.put("begin", null);
            Integer totalUserNumber = userMapper.getUserNumber(map);
            totalUserList.add(totalUserNumber);
            begin = begin.plusDays(1);
        }

        return UserReportVO.builder()
                .dateList(StringUtils.join(dateList, ","))
                .totalUserList(StringUtils.join(totalUserList, ","))
                .newUserList(StringUtils.join(newUserList, ","))
                .build();
    }

    @Override
    public OrderReportVO ordersStatistics(LocalDate begin, LocalDate end) {
        List<LocalDate> dateList = new ArrayList<>();
        Double orderCompletionRate;
        List<Integer> orderCountList = new ArrayList<>();
        List<Integer> validOrderCountList = new ArrayList<>();
        Integer totalOrderCount = 0;
        Integer validOrderCount = 0;
        while (!begin.isAfter(end)) {
            dateList.add(begin);
            // 获取每日订单数
            Map map = new HashMap();
            map.put("begin", LocalDateTime.of(begin, LocalTime.MIN));
            map.put("end", LocalDateTime.of(begin, LocalTime.MAX));
            Integer orderCount = ordersMapper.getOrdersCount(map);
            orderCountList.add(orderCount);
            totalOrderCount += orderCount;
            // 获取每日有效订单数
            map.put("status", Orders.COMPLETED);
            Integer validCount = ordersMapper.getOrdersCount(map);
            validOrderCountList.add(validCount);
            validOrderCount += validCount;

            begin = begin.plusDays(1);
        }
//        orderCountList.stream().reduce(new BinaryOperator<Integer>() {
//            @Override
//            public Integer apply(Integer integer1, Integer integer2) {
//                return integer1 + integer2;
//            }
//        });
//        orderCountList.stream().reduce((integer1, integer2) -> integer1 + integer2);
//        orderCountList.stream().reduce(Integer::sum);

        orderCompletionRate = (validOrderCount + 0.0) / totalOrderCount;

        return OrderReportVO.builder()
                .dateList(StringUtils.join(dateList, ","))
                .orderCompletionRate(orderCompletionRate)
                .orderCountList(StringUtils.join(orderCountList, ","))
                .totalOrderCount(totalOrderCount)
                .validOrderCount(validOrderCount)
                .build();
    }

    @Override
    public SalesTop10ReportVO top10(LocalDate begin, LocalDate end) {
        Map map = new HashMap();
        map.put("begin", LocalDateTime.of(begin, LocalTime.MIN));
        map.put("end", LocalDateTime.of(end, LocalTime.MAX));
        map.put("status", Orders.COMPLETED);
        List<Map> res = ordersMapper.getTop10(map);
        log.info("res: {}", res);
        List nameList = new ArrayList<>();
        List numberList = new ArrayList<>();
        res.forEach(r -> {
            Object name = r.get("name");
            Object sumNumber = r.get("sumNumber");
            // 获取姓名
            nameList.add(name);
            // 获取订单数量
            numberList.add(sumNumber);
        });
        return SalesTop10ReportVO.builder()
                .nameList(StringUtils.join(nameList, ","))
                .numberList(StringUtils.join(numberList, ","))
                .build();
    }
}
